<template>
  <div class="category-audit-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>IP分类申请审核</span>
          <div class="header-right">
            <el-select v-model="filterStatus" placeholder="状态筛选" clearable>
              <el-option
                v-for="(label, value) in statusMap"
                :key="value"
                :label="label"
                :value="value"
              />
            </el-select>
          </div>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="filteredApplications"
        style="width: 100%"
      >
        <el-table-column type="expand">
          <template #default="{ row }">
            <el-form label-position="left" inline class="application-detail">
              <el-form-item label="IP描述">
                <span>{{ row.description }}</span>
              </el-form-item>
              <el-form-item label="IP来源">
                <span>{{ row.source }}</span>
              </el-form-item>
              <el-form-item label="补充说明">
                <span>{{ row.remarks || '无' }}</span>
              </el-form-item>
              <el-form-item label="授权证明">
                <div class="license-files">
                  <el-image
                    v-for="(file, index) in row.license"
                    :key="index"
                    :src="file.url"
                    :preview-src-list="row.license.map(f => f.url)"
                    fit="cover"
                    class="license-image"
                  >
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Document /></el-icon>
                        {{ file.name }}
                      </div>
                    </template>
                  </el-image>
                </div>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        
        <el-table-column prop="id" label="申请ID" width="80" />
        <el-table-column prop="merchantName" label="申请商户" />
        <el-table-column prop="name" label="IP名称" />
        <el-table-column prop="type" label="IP类型">
          <template #default="{ row }">
            {{ typeMap[row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="statusTypeMap[row.status]">
              {{ statusMap[row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" width="180">
          <template #default="{ row }">
            {{ new Date(row.createTime).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button-group v-if="row.status === 'pending'">
              <el-button
                type="primary"
                link
                @click="handleReview(row, true)"
              >通过</el-button>
              <el-button
                type="danger"
                link
                @click="handleReview(row, false)"
              >拒绝</el-button>
            </el-button-group>
            <span v-else>{{ row.reason || '-' }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 审核对话框 -->
    <el-dialog
      :title="reviewType ? '通过申请' : '拒绝申请'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form ref="reviewFormRef" :model="reviewForm" :rules="reviewRules">
        <el-form-item
          label="审核意见"
          prop="reason"
          :rules="[
            { required: !reviewType, message: '拒绝时必须填写理由', trigger: 'blur' },
            { max: 200, message: '最多200个字符', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model="reviewForm.reason"
            type="textarea"
            rows="3"
            :placeholder="reviewType ? '可选填写通过理由' : '请填写拒绝理由'"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitReview" :loading="submitting">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Document } from '@element-plus/icons-vue'

// 模拟数据
const mockApplications = [
  {
    id: 1,
    merchantId: 1,
    merchantName: '动漫之家',
    name: '海贼王',
    type: 'anime',
    description: '日本超人气海盗题材漫画作品',
    source: '正版授权',
    status: 'approved',
    createTime: '2024-01-01T00:00:00Z',
    reviewTime: '2024-01-02T00:00:00Z',
    reason: '申请材料完整，IP价值高',
    license: [
      { name: '授权书.pdf', url: 'https://example.com/license1.pdf' }
    ],
    remarks: '已获得日本版权方授权'
  },
  {
    id: 2,
    merchantId: 2,
    merchantName: '游戏工作室',
    name: '英雄联盟',
    type: 'game',
    description: '全球最受欢迎的MOBA游戏之一',
    source: '授权申请中',
    status: 'pending',
    createTime: '2024-01-03T00:00:00Z',
    reviewTime: null,
    reason: null,
    license: [
      { name: '合作意向书.pdf', url: 'https://example.com/license2.pdf' }
    ],
    remarks: '正在与拳头公司洽谈授权事宜'
  },
  {
    id: 3,
    merchantId: 3,
    merchantName: '影视文化',
    name: '流浪地球',
    type: 'movie',
    description: '中国科幻电影代表作',
    source: '原创IP',
    status: 'rejected',
    createTime: '2024-01-04T00:00:00Z',
    reviewTime: '2024-01-05T00:00:00Z',
    reason: '授权证明材料不完整',
    license: [],
    remarks: '申请使用电影周边授权'
  }
]

const loading = ref(false)
const submitting = ref(false)
const dialogVisible = ref(false)
const filterStatus = ref('')
const applications = ref(mockApplications)
const currentApplication = ref(null)
const reviewType = ref(true) // true: 通过, false: 拒绝
const reviewFormRef = ref(null)

const reviewForm = ref({
  reason: ''
})

const typeMap = {
  anime: '动漫IP',
  game: '游戏IP',
  movie: '影视IP',
  literature: '文学IP',
  other: '其他'
}

const statusMap = {
  pending: '待审核',
  approved: '已通过',
  rejected: '已拒绝'
}

const statusTypeMap = {
  pending: 'warning',
  approved: 'success',
  rejected: 'danger'
}

// 根据状态筛选申请列表
const filteredApplications = computed(() => {
  if (!filterStatus.value) return applications.value
  return applications.value.filter(item => item.status === filterStatus.value)
})

// 打开审核对话框
const handleReview = (row, isApprove) => {
  currentApplication.value = row
  reviewType.value = isApprove
  reviewForm.value.reason = ''
  dialogVisible.value = true
}

// 提交审核
const submitReview = async () => {
  if (!reviewFormRef.value) return

  await reviewFormRef.value.validate(async (valid) => {
    if (valid) {
      submitting.value = true
      try {
        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000))
        
        const index = applications.value.findIndex(
          item => item.id === currentApplication.value.id
        )
        
        if (index !== -1) {
          applications.value[index] = {
            ...applications.value[index],
            status: reviewType.value ? 'approved' : 'rejected',
            reviewTime: new Date().toISOString(),
            reason: reviewForm.value.reason
          }
        }

        ElMessage.success('审核完成')
        dialogVisible.value = false
      } catch (error) {
        ElMessage.error('审核失败')
      } finally {
        submitting.value = false
      }
    }
  })
}
</script>

<style scoped>
.category-audit-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-right {
  display: flex;
  gap: 10px;
}

.application-detail {
  padding: 20px;
  background-color: var(--el-fill-color-light);
  border-radius: 4px;
}

.application-detail :deep(.el-form-item) {
  display: flex;
  margin-right: 0;
  margin-bottom: 10px;
  width: 100%;
}

.application-detail :deep(.el-form-item__label) {
  width: 100px;
  color: var(--el-text-color-secondary);
}

.license-files {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.license-image {
  width: 100px;
  height: 100px;
  border-radius: 4px;
  border: 1px solid var(--el-border-color);
}

.image-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--el-text-color-secondary);
  font-size: 12px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 